﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using VPLink2014.Data.MesApplis;

namespace VPLink2014
{
    public partial class GestionMesApplicationsManager : Page
    {
        private void AddDirectionName(string directionName)
        {
            if (!string.IsNullOrEmpty(directionName) && directionName != ".")
            {
                pnlFilter.Controls.Add(new Literal()
                {
                    Text = string.Format(@"<input type=""hidden"" id=""maDirection"" value=""{0}"">", directionName)
                });
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    //SPUser currentSPUser = SPContext.Current.Web.CurrentUser;
                    SPUser currentSPUser = SPControl.GetContextWeb(this.Context).CurrentUser;
                    if (currentSPUser != null)
                    {
                        pnlFilter.Controls.Add(new Literal()
                        {
                            Text = string.Format("<input type='hidden' id='loginname' value='{0}'>", currentSPUser.LoginName)
                        });
                        string userName = currentSPUser.LoginName;
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                            VOUserMapper userMapper = new VOUserMapper(userName);
                            if (userMapper != null)
                            {
                                //User currentUser = userMapper.getUserByName( currentSPUser.Name );
                                if (userMapper.user != null)
                                {
                                    if (userMapper.user.isManager == true)
                                    {
                                        pnlFilter.Controls.Add(new Literal() { Text = "<input type='hidden' id='isManager' value='true'>" });
                                        AddDirectionName(userMapper.user.direction);
                                        if (!string.IsNullOrEmpty(userMapper.user.service)
                                            && userMapper.user.service != ".")
                                        {
                                            pnlFilter.Controls.Add(new Literal() { Text = @"<input type=""hidden"" id=""filterType"" value=""service"">" });
                                            pnlFilter.Controls.Add(new Literal() { Text = string.Format(@"<input type=""hidden"" id=""filterValue"" value=""{0}"">", userMapper.user.service) });
                                        }
                                        else
                                        {
                                            if (!string.IsNullOrEmpty(userMapper.user.departement)
                                                && userMapper.user.departement != ".")
                                            {
                                                pnlFilter.Controls.Add(new Literal() { Text = "<input type='hidden' id='filterType' value='departement'>" });
                                                pnlFilter.Controls.Add(new Literal() { Text = string.Format(@"<input type=""hidden"" id=""filterValue"" value=""{0}"">", userMapper.user.departement) });
                                            }
                                            else
                                            {
                                                if (!string.IsNullOrEmpty(userMapper.user.direction)
                                                    && userMapper.user.direction != ".")
                                                {
                                                    pnlFilter.Controls.Add(new Literal() { Text = "<input type='hidden' id='filterType' value='direction'>" });
                                                    pnlFilter.Controls.Add(new Literal() { Text = string.Format(@"<input type=""hidden"" id=""filterValue"" value=""{0}"">", userMapper.user.direction) });
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        ApplicationManager.Visible = false;
                                        RedirectToUserPage.Visible = true;
                                    }
                                }
                                else
                                {
                                    ApplicationManager.Visible = false;
                                    pnlFilter.Controls.Add(new Literal()
                                    {
                                        Text = string.Format("<p>Votre compte n'a pas été trouvé.</p>")
                                    });
                                }
                            }
                            else
                            {
                                ApplicationManager.Visible = false;
                                pnlFilter.Controls.Add(new Literal()
                                {
                                    Text = string.Format("<p>Votre compte n'a pas été trouvé.</p>")
                                });
                            }
                        });
                    }
                    else
                    {
                        pnlFilter.Controls.Add(new Literal()
                        {
                            Text = string.Format("<p>Votre compte n'a pas été trouvé.</p>")
                        });
                    }
                }
                catch (Exception ex)
                {
                    ApplicationManager.Visible = false;
                    pnlFilter.Controls.Add(new Literal()
                    {
                        Text = string.Format("<input type='hidden' id='error' value='{0}'>", ex.StackTrace)
                    });
                }
            }

            else
            {
                ApplicationManager.Visible = false;
                //Validation.Visible = true;
            }
        }
        protected void Page_Init()
        {
        }
    }
}
